package com.mfdm.gui.views;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.part.ViewPart;

import com.mfdm.gui.actions.AddNewSongAction;
import com.mfdm.gui.serversimulator.ServerSimulator;
import com.mfdm.gui.serversimulator.Song;
import com.mfdm.gui.utils.ResourcesUtils;

@SuppressWarnings("restriction")
public class ListObjectsView extends ViewPart {

	private static final String ADD_NEW_ACTION_IMAGE_PATH = "icons/addNewAction.gif";
	
	private static final String DELETE_ACTION_IMAGE_PATH = "icons/deleteAction.gif";

	public static final String ID = "my-documents-management.view.listobject";
	
	private List m_songsListControl;
	
	private Song m_songSelected;
	
	private Map<String, Song> m_mapData;
	
	private java.util.List<SongObserver> m_observers;

	private Action m_addNewSongAction;

	private Action m_deleteAction;
	
	@Override
	public void createPartControl(final Composite parent) {
		ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL|SWT.V_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		Composite dataComposite = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gridLayout = new GridLayout(2, true);
		dataComposite.setLayout(gridLayout);
		scrolledComposite.setContent(dataComposite);		
		
		// The first row		
		GridData gridData2 = new GridData(GridData.FILL, GridData.FILL, true, false);		
		gridData2.horizontalSpan = 2;
		final Text searchText = new Text(dataComposite, SWT.BORDER);
		searchText.setText("Type here to search...");
		searchText.setLayoutData(gridData2);
		
		// List control
		GridData listSongsGridData = new GridData(GridData.FILL, GridData.FILL, true, true);
		listSongsGridData.horizontalSpan = 2;	

		m_songsListControl = new List(dataComposite, SWT.FILL);
		m_songsListControl.setLayoutData(listSongsGridData);
		
		
		searchText.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				searchText.setText("");				
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				
			}
		});
		
		searchText.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				searchText.setText("Type here to search...");
				
			}
			
			@Override
			public void focusGained(FocusEvent e) {
			}
		});		
		scrolledComposite.setMinSize(dataComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));		
			
		createActions(parent);
		
		populateData();
		
		getAllObservers();
		
		m_songsListControl.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				m_songSelected = null;
				String[] selections = m_songsListControl.getSelection();
				if (selections.length >= 1) {
					m_songSelected = m_mapData.get(selections[selections.length -1]);					
				}
				
				if (m_songSelected != null) {
					notifySelectionChanged(m_songSelected);
				}
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

	}
	
	private void getAllObservers() {
		m_observers = new ArrayList<SongObserver>();		
		IWorkbenchPage workbenchPage = Workbench.getInstance().getActiveWorkbenchWindow().getActivePage();
		
		// ContentObjectViews
		IViewPart contentViewPart = workbenchPage.findView(ContentObjectViews.ID);
		SongObserver contentObserver = (SongObserver) contentViewPart;
		m_observers.add(contentObserver);
		
		
		
	}

	protected void notifySelectionChanged(Song songSelected) {

		for (SongObserver observer : m_observers) {
			observer.update(songSelected);
		}
	}

	private void populateData() {

		m_mapData = ServerSimulator.getSongs();
		
		for(Entry<String, Song> map : m_mapData.entrySet()) {
			m_songsListControl.add(map.getKey());
		}
	}

	private void createActions(final Composite parent) {

		m_addNewSongAction = new AddNewSongAction();
		m_addNewSongAction.setToolTipText("Add new song");		
		ImageDescriptor imageDescriptor = ResourcesUtils.getImage(ADD_NEW_ACTION_IMAGE_PATH);
		m_addNewSongAction.setImageDescriptor(imageDescriptor);		
		getViewSite().getActionBars().getToolBarManager().add(m_addNewSongAction);
		
		
		
		m_deleteAction = new Action("Delete") {
			@Override
			public void run() {				
				super.run();
				MessageDialog.openInformation(parent.getShell(), "Delete", "The object you selected is deleted");
			}
		};				
		m_deleteAction.setToolTipText("Delete song");		
		ImageDescriptor imageDescriptor2 = ResourcesUtils.getImage(DELETE_ACTION_IMAGE_PATH);
		m_deleteAction.setImageDescriptor(imageDescriptor2);	
		getViewSite().getActionBars().getToolBarManager().add(m_deleteAction);		
		
	}

	@Override
	public void setFocus() {

	}
}
